﻿var myapp = myapp || {};

myapp.viewModel = function () {
    var self = this;
    self.data = new spa.dataContext(true);
    self.set = self.data.AddSet("Products", "ID");
    self.view = self.set.CreateView();
    self.view.Query.filter = "Quantity " + spa.filterTypes.GreaterThan + " 0";
    self.view.Query.orderby.push(new spa.ordering("Price"));

    self.selectedItem = ko.observable();

    // Refresh our View
    self.view.Refresh();

    self.addItem = function () {
        self.view.AddItem(new spa.dataEntity({ ID: 0, Name: "<New Item>", Price: 0, Quantity: 0, Category: "" }));
    };
    self.deleteItem = function (item) {
        self.view.DeleteItem(item);
    };
    self.save = function () {
        self.view.SaveAll();
    };
    self.refresh = function () {
        self.view.Refresh(false);
    };

    return {
        Items: self.view.ViewItems,
        SelectedItem: self.selectedItem,
        Refresh: self.refresh,
        AddItem: self.addItem,
        Save: self.save,
        Delete: self.deleteItem,
    }
};

$(document).ready(function () {
    ko.applyBindings(new myapp.viewModel());
});

